package com.houtai.controller.role;


import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.RetInfo;
import com.houtai.bean.TsJzAdmin;
import com.houtai.bean.TsJzRole;
import com.houtai.service.funcion.FuncionService;
import com.houtai.service.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping(value = {"v5"})
public class RoleController {

    @Resource
    private FuncionService funcionService;
    @Resource
    private RoleService roleService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Purpose  查找角色
     * @param
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   ModelAndView
     */
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/admin/role/list")
    public ModelAndView list(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("role/index");
        mv.addObject("token", request.getParameter("token"));
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        mv.addObject("funcions", funcionService.findSonFuncion(tsJzAdmin, Integer.valueOf(request.getParameter("father_id"))));
        mv.addObject("roles", roleService.findRoles(tsJzAdmin));
        return mv;
    }
    /**
     * @Purpose  跳转到添加角色页面
     * @param
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   ModelAndView
     */
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/admin/role/insert/jump")
    public ModelAndView insertj(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("role/insert");
        mv.addObject("token", request.getParameter("token"));
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        mv.addObject("functions", funcionService.findFuncionByRole(tsJzAdmin, null));
        return mv;
    }
    /**
     * @Purpose  跳转到更新角色页面
     * @param
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   ModelAndView
     */
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/admin/role/update/jump")
    public ModelAndView updatej(HttpServletRequest request, @RequestParam(value = "role_id" , required = true)int role_id ) {
        ModelAndView mv = new ModelAndView("role/update");
        mv.addObject("token", request.getParameter("token"));
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        TsJzRole tsJzRole = roleService.findRoleById(role_id);
        mv.addObject("role", tsJzRole);
        mv.addObject("functions", funcionService.findFuncionByRole(tsJzAdmin, tsJzRole));
        return mv;
    }
    /**
     * @Purpose  添加角色
     * @param    tsJzRole
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   RetInfo
     */
    @RequestMapping(value="/admin/role/insert", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo insert(@RequestBody TsJzRole tsJzRole, HttpServletRequest request){

        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        return roleService.insertRole(tsJzAdmin, tsJzRole , request);

    }
    /**
     * @Purpose  更新角色
     * @param    tsJzRole
     * @version  3.4.0
     * @author   lizhun
     * @time     2018-07-10
     * @return   RetInfo
     */
    @RequestMapping(value="/admin/role/update", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo update(@RequestBody TsJzRole tsJzRole, HttpServletRequest request){

        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        return roleService.updateRole(tsJzAdmin, tsJzRole , request);

    }
}
